//
//  CategoryViewController.m
//  TWH-Asst3
//
//  Created by betopp on 3/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CategoryViewController.h"
#import "ColorPickerView.h"

@implementation CategoryViewController

@synthesize tableView;
@synthesize viewCat;
@synthesize noteView;
@synthesize toolBar;

@synthesize nameField;

-(void)addPressed{
	
	Note *n = [[Note alloc]init];
	[viewCat.notesArray addObject:n];
	noteView.viewNote = n;
	
	self.editing = NO;
	[self.navigationController pushViewController:noteView animated:YES];
	[nameField resignFirstResponder];
	
}

-(void)backPressed{
	[self.navigationController popViewControllerAnimated:YES];
	[nameField resignFirstResponder];
}

-(void)deletePressed{
	viewCat.deleteMe = YES;
	[self.navigationController popViewControllerAnimated:YES];
	[nameField resignFirstResponder];
}

-(void)renamePressed{
	NSLog(@"rename pressed");
	nameField.text = viewCat.categoryName;
	[nameField becomeFirstResponder];
	
}
-(void)colorCallback{
	NSLog(@"ccb");
	
	self.viewCat.categoryColor = cpv.selectedColor;
	
}
-(void)colorPressed{
	
	cpv.selectedColor = viewCat.categoryColor;
	
	cpv.callobj = self;
	
	[nameField resignFirstResponder];
	[self.navigationController pushViewController:cpv animated:YES];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
	[textField resignFirstResponder];
	
	self.viewCat.categoryName = textField.text;
	[self.navigationItem setTitle:viewCat.categoryName];
	
	return NO;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
	self.viewCat.categoryName = [textField.text stringByReplacingCharactersInRange:range withString:string];
	[self.navigationItem setTitle:self.viewCat.categoryName];
	
	return YES;
}


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	
	cpv = [[ColorPickerView alloc]initWithNibName:@"ColorPickerView" bundle:nil];
	
	UIBarButtonItem *addbtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPressed)];
	[addbtn autorelease];
	self.navigationItem.rightBarButtonItem = addbtn;	
	
	noteView = [[NoteViewController alloc]initWithNibName:@"NoteViewController" bundle:nil];
	
	UIBarButtonItem *labelbtn = [[UIBarButtonItem alloc] initWithTitle:@"Category:" style:UIBarButtonItemStylePlain target:self action:nil];
	
	
	UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
	
	
	UIBarButtonItem *delbtn = [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(deletePressed)];
	UIBarButtonItem *renbtn = [[UIBarButtonItem alloc] initWithTitle:@"Rename" style:UIBarButtonItemStyleBordered target:self action:@selector(renamePressed)];
	UIBarButtonItem *colbtn = [[UIBarButtonItem alloc] initWithTitle:@"Color" style:UIBarButtonItemStyleBordered target:self action:@selector(colorPressed)];
	
	
	self.toolbarItems = [NSArray arrayWithObjects:labelbtn,spacer,delbtn,renbtn,colbtn,nil];
	
	
}



- (void)viewWillAppear:(BOOL)animated {
   	[super viewWillAppear:animated];
	
	[self.navigationItem setTitle:viewCat.categoryName];
	
	int i;
	for(i=0;i<[viewCat.notesArray count];i++)
	{
		if([[viewCat.notesArray objectAtIndex:i] deleteMe] == YES)
		{
			[viewCat.notesArray removeObjectAtIndex:i];
			i--;
			
		}
	}
	
	[tableView reloadData];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [viewCat.notesArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	
	//	NoteCategory *cat = [categoriesArray objectAtIndex:[indexPath row]];
	
	Note *n = [viewCat.notesArray objectAtIndex:[indexPath row]];
	
	UIView *cellview = [[UIView alloc] init];
	cellview.backgroundColor = [colorValuesArray objectAtIndex:n.noteColor];
	
	cell.textLabel.text = n.noteTitle;
	cell.textLabel.textColor = [UIColor whiteColor];
	cell.backgroundView = cellview;
	cell.textLabel.backgroundColor = [colorValuesArray objectAtIndex:n.noteColor];
	
	
	
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
	/*
	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
	 [self.navigationController pushViewController:detailViewController animated:YES];
	 [detailViewController release];
	 */
	
	//self.catView.viewCat = [categoriesArray objectAtIndex:indexPath.row];
	
	self.noteView.viewNote = [viewCat.notesArray objectAtIndex:indexPath.row];
	[self.navigationController pushViewController:self.noteView animated:YES];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

